<template>
  <div>
    <div class="login-header">
      <div class="container">
        <div class="logo">
          <a href="/index"
            ><img
              src="/imgs/mi-logo.png" width="60px" height="60px"
              alt=""
          /></a>
        </div>
      </div>
      <div class="login-body">
        <div class="container">
          <div class="login-content">
            <div class="nav_tabs_panel">
              <div id="nav-tabs" class="nav_tabs">
                <a
                  class="navtab-link now"
                  href="javascript:void(0);"
                  data-tab="pwd"
                  >帐号登录</a
                ><span class="line"></span>
                <a class="navtab-link" href="javascript:void(0);" data-tab="qr"
                  >扫码登录</a
                >
              </div>
            </div>
            <div class="login-box">
              <div class="login-item">
                <input
                  type="text"
                  placeholder="请输入用户名"
                  v-model="username"
                />
              </div>
              <div class="login-item">
                <input type="password" placeholder="密码" v-model="pwd" />
              </div>
              <div class="login-item errmsg">{{ errmessage }}</div>
              <div class="login-item btn-login">
                <a href="javascript:;" @click="login">登陆</a>
              </div>
              <div class="login-item msg-login">
                <a href="#">手机短信登陆</a>
              </div>
              <div class="login-item reg-forget">
                <a href="#">立即注册 | 忘记密码？</a>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <navfooter></navfooter>
  </div>
</template>

<script>
import navfooter from "./../components/navfooter";
import qs from "qs";
import { mapActions } from "vuex";

export default {
  components: {
    navfooter,
  },
  data() {
    return {
      username: "",
      pwd: "",
      userid: "",
      errmessage: "",
    };
  },
  methods: {
    login() {
      if (this.username == "" || this.pwd == "") {
        this.errmessage = "用户名或密码不能为空";
      } else {
        let { username, pwd } = this;
        var param = {
          username: username,
          pwd: pwd,
        };
        this.axios
          .post("/user/login", qs.stringify(param))
          .then((res) => {
            if (res.length != 0) {
              this.errmessage = "";
              this.$cookie.set("uid", res[0].uid, "1m");
              //this.$store.dispatch("saveUserName", res[0].username);
              //也可以
              this.saveUserName(res[0].username);
              this.saveUserId(res[0].uid);
              this.$router.push("/index");
            } else {
              this.errmessage = "用户名或密码错误";
            }
          })
          .catch((err) => {
            this.errmessage = err;
          });
      }
    },
    ...mapActions(["saveUserName", "saveUserId"]), //和第94行对应
  },
};
</script>

<style lang="scss">
.login-body {
  width: 100%;
  background-image: url("https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/aece569d6689b4c461bb53efd9eea9c7.jpg");
  height: 588px;
  position: relative;
  left: 0;
  background-repeat: no-repeat;
  background-position: top center;
  .login-content {
    position: absolute;
    background-color: #fff;
    right: 0px;
    top: 40px;
    width: 410px;
    height: 400px;
    .nav_tabs_panel {
      .nav_tabs {
        padding: 27px 0 24px;
        text-align: center;
        font-size: 24px;
        color: #666;
        .now {
          color: #f56600;
        }
        .line {
          width: 1px;
          height: 24px;
          margin: 0 35px 0 42px;
          border: 1px solid #e0e0e0;
          background: #e0e0e0;
        }
        a {
          color: #666;
        }
      }
    }
    .login-box {
      width: 348px;
      padding-bottom: 0;
      margin: 0 auto;
      .btn-login {
        a {
          display: inline-block;
          width: 342px;
          height: 50px;
          background-color: #ff6600;
          color: #fff;
          text-align: center;
          line-height: 50px;
          font-size: 16px;
        }
      }
      .msg-login {
        width: 342px;
        text-align: center;
        a {
          color: #ff6600;
          font-size: 14px;
        }
      }
      .reg-forget {
        width: 342px;
        text-align: center;
        a {
          color: #999;
          font-size: 14px;
        }
      }
      .errmsg {
        color: #ff6600;
        font-size: 14px;
      }
      .login-item {
        margin-bottom: 20px;

        input {
          width: 306px;
          height: 22px;
          line-height: 22px;
          padding: 13px 16px 13px 14px;
          display: block;
          outline: none;
        }
      }
    }
  }
}
</style>